import {defineStore} from 'pinia'
import {userApi} from '@/api'
import type {UserVO} from '@/types/vo/user'

interface UserState {
    userProfile: UserVO | null
    loading: boolean
}

export const useUserStore = defineStore('user', {
    state: (): UserState => ({
        userProfile: null,
        loading: false,
    }),

    getters: {
        hasProfile: (state) => !!state.userProfile,
        isAdmin: (state) => state.userProfile?.userRole === 'admin',
    },

    actions: {
        /**
         * 获取当前用户信息
         */
        async fetchCurrentUser() {
            try {
                this.loading = true
                const user = await userApi.getCurrentUser()
                this.userProfile = user
                return user
            } finally {
                this.loading = false
            }
        },

        /**
         * 更新用户资料
         */
        async updateProfile(payload: any) {
            try {
                this.loading = true
                const user = await userApi.updateProfile(payload)
                this.userProfile = user
                return user
            } finally {
                this.loading = false
            }
        },

        /**
         * 重置密码
         */
        async resetPassword(payload: any) {
            await userApi.resetPassword(payload)
        },

        /**
         * 清除用户信息
         */
        clearUser() {
            this.userProfile = null
        },
    },
})




